package com.example.base.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.base.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NestedListview extends ListView implements AbsListView.OnScrollListener {
  private Context mContext;
  private View mFootView;
  private int mTotalItemCount;
  private int mFirstItem;
  private OnLoadMoreListener mLoadMoreListener;
  private boolean mIsLoading = false;
  @BindView(R.id.pull_pre_img)
  ImageView pullPreImg;
  @BindView(R.id.pull_tv)
  TextView pullTv;
  @BindView(R.id.pull_next_img)
  ProgressBar pullNextImg;
  private View headView;
  private OnRefreshListener onRefreshListener;

  public NestedListview(Context context) {
    super(context);
    init(context);
    initHead(context);
  }

  public NestedListview(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
    initHead(context);
  }

  public NestedListview(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
    initHead(context);
  }

  private void init(Context context) {
    this.mContext = context;
    mFootView = LayoutInflater.from(context).inflate(R.layout.load, null);
    setOnScrollListener(this);
  }

  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
    int lastVisibleIndex = view.getLastVisiblePosition();
    if (!mIsLoading && scrollState == OnScrollListener.SCROLL_STATE_IDLE
        && lastVisibleIndex == mTotalItemCount - 1) {
      mIsLoading = true;
      addFooterView(mFootView);
      if (mLoadMoreListener != null) {
        mLoadMoreListener.onloadMore();
      }
    }
    int firstVisibleIndex = view.getFirstVisiblePosition();
    if (!mIsLoading && scrollState == OnScrollListener.SCROLL_STATE_IDLE
        && firstVisibleIndex == 0) {
      Log.i("listadpter", firstVisibleIndex + "");
      Log.i("listadpter", mFirstItem + "");
      mIsLoading = true;
      addHeaderView(headView);
      if (onRefreshListener != null) {
        onRefreshListener.refreshData();
      }
    }

  }

  @Override
  public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    mTotalItemCount = totalItemCount;
    mFirstItem = firstVisibleItem;
  }

  public void setOnLoadMoreListener(OnLoadMoreListener listener) {
    mLoadMoreListener = listener;
  }

  public interface OnLoadMoreListener {
    void onloadMore();
  }

  public void setLoadCompleted() {
    mIsLoading = false;
    removeFooterView(mFootView);
  }


  public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
    this.onRefreshListener = onRefreshListener;
  }


  public void initHead(Context context) {
    this.mContext = context;
    headView = LayoutInflater.from(context).inflate(R.layout.pull_lay, null);
    setOnScrollListener(this);

  }

  public void onRefreshComplate() {
    mIsLoading = false;
    removeHeaderView(headView);
  }

  public interface OnRefreshListener {
    void refreshData();
  }


}

